package com.dure.hotmemory.scenes.dzxl;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.EmbossMaskFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;

import com.dure.commonlib.util.AssetsUtil;
import com.dure.commonlib.util.PreferencesUtils;
import com.dure.hotmemory.bean.Cell;
import com.dure.hotmemory.bean.Code;
import com.dure.hotmemory.drawview.BaseSurfaceView;
import com.dure.hotmemory.scenes.BaseScene;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dure on 2017/5/31.
 */

public class ScanScene extends BaseScene {
    private List<Code> codeList = new ArrayList<>();
    Paint paint;
    private int startIndex, endIndex, currentIndex;
    private boolean showNext = true;
    private Bitmap currentBitmap;
    private String currentName;
    private int bitmapWidth, bitmapHeight;
    private Rect srcRect;
    private long startTime;
    private boolean dataPrepared;
    private float changeTime;
    private boolean trainingFinished;
    private RectF leftBtnRectF, rightBtnRectF;
    private String finishInfo;
    public boolean restart = true;
    private EmbossMaskFilter emboss;
    int[] colors = {Color.WHITE, Color.parseColor("#00BDFF")};
    public String alias;

    public ScanScene(BaseSurfaceView baseSurfaceView) {
        super(baseSurfaceView);
        left = getViewWidth();
        paint = new Paint();
        paint.setAntiAlias(true);

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N_MR1) {
            float[] direction = new float[] { 1, 1, 1 };
            // 设置环境光亮度
            float light = 0.5f;
            // 选择要应用的反射等级
            float specular = 10;// 这个参数更改没有看出效果上面的变化
            // 向mask应用一定级别的模糊
            float blur = 3;// 这个参数可以控制立体侧面的宽度
            emboss = new EmbossMaskFilter(direction, light, specular, blur);
            paint.setMaskFilter(emboss);
        }

        alias = PreferencesUtils.getString(activity, "current_alias");
        changeTime = PreferencesUtils.getFloat(activity, alias+":change_time", 1);
        currentIndex = startIndex = PreferencesUtils.getInt(activity, alias+":start_index", 0);
        endIndex = PreferencesUtils.getInt(activity, alias+":end_index", 9);
        dataInited = true;
    }

    public void setCodeList(List<Code> codeList) {
        if (codeList != null && codeList.size() > 0) {
            this.codeList.clear();
            this.codeList.addAll(codeList);
        }
    }

    @Override
    public void handleData() {
        if (!dataInited) {
            return;
        }
        if (codeList.size() == 0) {
            return;
        }
        if (restart) {
            restart = false;
            currentIndex = startIndex;
        }
        if (showNext) {
            showNext = false;
            Code code = codeList.get(currentIndex);

            if (!code.isCreated)
                currentBitmap = BitmapFactory.decodeStream(AssetsUtil.getAssets(context, code.image));
            else{
                FileInputStream is = null;
                try {
                    is = new FileInputStream(code.image);
                    currentBitmap = BitmapFactory.decodeFileDescriptor(is.getFD(), null, null);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            currentName = code.number + " - " + code.name;
            bitmapHeight = currentBitmap.getHeight();
            bitmapWidth = currentBitmap.getWidth();

            srcRect = new Rect(0, 0, bitmapWidth, bitmapHeight);
            startTime = System.currentTimeMillis();
            dataPrepared = true;
        }

        if (!trainingFinished && !showNext && System.currentTimeMillis() - startTime > changeTime * 1000) {
            if (currentIndex < endIndex) {
                showNext = true;
                currentIndex++;
            } else {
                finishInfo = "浏览结束, 是否要再来一次？";
                trainingFinished = true;
            }
        }


    }

    @Override
    public void handleDraw(Canvas canvas) {
        if (!dataInited) {
            return;
        }
        if (codeList.size() == 0) {
            return;
        }

        if (!dataPrepared) {
            return;
        }


        if (trainingFinished) {
            drawFinishDialog(canvas);
        } else {
            drawCenterView(canvas);
        }
//        drawBottomView(canvas);
    }

    private void drawCenterView(Canvas canvas) {
        int centerX = canvas.getWidth() / 2;
        int centerY = canvas.getHeight() / 2;
//        int dstWidth = (int) (canvas.getWidth() * 0.7f);


        int dstLeft = centerX - currentBitmap.getWidth() / 2;
        int dstTop = centerY - currentBitmap.getHeight() / 2;
        int dstRight = centerX + currentBitmap.getWidth() / 2;
        int dstBottom = centerY + currentBitmap.getHeight() / 2;
        RectF dstRectF = new RectF(dstLeft, dstTop, dstRight, dstBottom);
        float arc = dstRectF.width() / 15;
        canvas.drawRoundRect(dstRectF, arc, arc, paint);
        drawShowingBitmap(canvas, currentBitmap, dstRectF);


        int textSize = centerX / 4;
        paint.setColor(Color.WHITE);
        paint.setTextSize(textSize);
        float nameWidth = paint.measureText(currentName);
        float nameLeft = centerX - nameWidth / 2;
        float nameBottom = dstBottom + textSize * 1.5f;
        canvas.drawText(currentName, nameLeft, nameBottom, paint);

        textSize = centerX / 8;
        paint.setTextSize(textSize);
        String indexText = (currentIndex - startIndex + 1) + "/" + (endIndex - startIndex + 1);
        float indexWidth = paint.measureText(indexText);
        float indexLeft = centerX - indexWidth / 2;
        float indexBottom = nameBottom + textSize * 2f;
        canvas.drawText(indexText, indexLeft, indexBottom, paint);
//        paint.setMaskFilter(null);
    }

    private void drawShowingBitmap(Canvas canvas, Bitmap bitmap, RectF dstRectF) {
        Rect srcRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        float padding = dstRectF.width() * 0.1f;
        float dstLeft = dstRectF.left + padding;
        float dstTop = dstRectF.top + padding;
        float dstRight = dstRectF.right - padding;
        float dstBottom = dstRectF.bottom - padding;
        RectF newDstRect = new RectF(dstLeft, dstTop, dstRight, dstBottom);
        canvas.drawBitmap(bitmap, srcRect, newDstRect, null);
    }


    private void drawFinishDialog(Canvas canvas) {
        String infoStr = finishInfo;
        String exitStr = "退出";
        String continueStr = "好的";

        float dialogWidth = canvas.getWidth() * 0.8f;
        float dialogHeight = dialogWidth * 0.6f;
        float infoPartHeight = dialogHeight * 0.7f;
        float btnPartHeight = dialogHeight * 0.3f;

        float infoTextSize = dialogWidth * 0.8f / infoStr.length();

        // 绘制dialog frame
        float dialogLeft = canvas.getWidth() / 2 - dialogWidth / 2;
        float dialogTop = canvas.getHeight() / 2 - dialogHeight / 2;
        float dialogRight = canvas.getWidth() / 2 + dialogWidth / 2;
        float dialogBottom = canvas.getHeight() / 2 + dialogHeight / 2;
        RectF dialogRectF = new RectF(dialogLeft, dialogTop, dialogRight, dialogBottom);
        paint.setColor(Color.WHITE);
        float arc = dialogRectF.width() / 15;

        LinearGradient lg = new LinearGradient(dialogRectF.width() /2, 0, dialogRectF.width() /2, dialogRectF.height(), colors, null, Shader.TileMode.MIRROR);
        paint.setShader(lg);
        canvas.drawRoundRect(dialogRectF, arc, arc, paint);
        paint.setShader(null);

        // 绘制info
        paint.setTextSize(infoTextSize);
        float infoWidth = paint.measureText(infoStr);
        float infoLeft = canvas.getWidth() / 2 - infoWidth / 2;
        float infoBottom = dialogTop + infoPartHeight / 2 + infoTextSize / 2;
        paint.setColor(Color.RED);
        canvas.drawText(infoStr, infoLeft, infoBottom, paint);

        float btnWidth = dialogWidth * 0.3f;
        float btnHeight = btnWidth / 2.5f;
        float btnTextSize = btnHeight * 0.6f;

        // 绘制左边btn
        float leftBtnLeft = canvas.getWidth() / 2 - dialogWidth / 4 - btnWidth / 2;
        float leftBtnBottom = dialogBottom - btnPartHeight / 2 + btnHeight / 2;
        float leftBtnRight = leftBtnLeft + btnWidth;
        float leftBtnTop = leftBtnBottom - btnHeight;
        leftBtnRectF = new RectF(leftBtnLeft, leftBtnTop, leftBtnRight, leftBtnBottom);
        arc = leftBtnRectF.width() / 15;
        paint.setColor(Color.parseColor("#0099ff"));
        canvas.drawRoundRect(leftBtnRectF, arc, arc, paint);

        paint.setTextSize(btnTextSize);
        float exitStrWidth = paint.measureText(exitStr);
        float exitLeft = leftBtnLeft + btnWidth / 2 - exitStrWidth / 2;
        float exitBottom = leftBtnBottom - btnHeight * 0.3f;
        paint.setColor(Color.WHITE);
        canvas.drawText(exitStr, exitLeft, exitBottom, paint);

        // 绘制右边btn
        float rightBtnLeft = canvas.getWidth() / 2 + dialogWidth / 4 - btnWidth / 2;
        float rightBtnBottom = leftBtnBottom;
        float rightBtnRight = rightBtnLeft + btnWidth;
        float rightBtnTop = leftBtnTop;
        rightBtnRectF = new RectF(rightBtnLeft, rightBtnTop, rightBtnRight, rightBtnBottom);
        arc = rightBtnRectF.width() / 15;
        paint.setColor(Color.parseColor("#0099ff"));
        canvas.drawRoundRect(rightBtnRectF, arc, arc, paint);

        paint.setTextSize(btnTextSize);
        float continueStrWidth = paint.measureText(continueStr);
        float continueLeft = rightBtnLeft + btnWidth / 2 - continueStrWidth / 2;
        float continueBottom = rightBtnBottom - btnHeight * 0.3f;
        paint.setColor(Color.WHITE);
        canvas.drawText(continueStr, continueLeft, continueBottom, paint);
    }

    @Override
    public void handleDown(PointF pointF) {

        if (trainingFinished) {
            if (leftBtnRectF.contains(pointF.x, pointF.y)) {
                activity.backToLastScene();
            } else if (rightBtnRectF.contains(pointF.x, pointF.y)) {
                trainingFinished = false;
                restart = true;
                showNext = true;
            }
        }
    }

    @Override
    public void handleUp(PointF pointF) {
        super.handleUp(pointF);
    }

}
